package models.mappers;

import enums.WdShareTaskStatus;
import models.WdShareTask;
import org.apache.ibatis.annotations.*;
import vos.BuyerTaskVo;
import vos.SellerBalanceVo;
import vos.TaskSearchVo;
import vos.WdShareTaskSearchVo;

import java.util.List;

/**
 * 微代任务分享任务
 * @author zhiyuan
 * @version v1.0
 * @since 2017年4月1日 下午9:28:29
 */
public interface WdShareTaskMapper {
	
	/**
	 * 更新任务状态 WAIT_SUBMIT->SUMITED
	 * @author 刘志远<yuanzhi-2@163.com>
	 * @since 2017年4月1日
	 * @param taskId
	 */
	@Update("update wd_share_task set status='SUMITED' where task_id = #{id} and status='WAIT_SUBMIT'")
	void submitTask(@Param("id") Long taskId);
	
	/**
	 * 领取任务
	 * @author 刘志远<yuanzhi-2@163.com>
	 * @since 2017年4月4日
	 * @param task
	 */
	@Insert("insert into wd_share_task(buyer_id, task_id, comminssion, status, take_time) values(#{buyerId}, #{taskId}, #{comminssion}, #{status}, #{takeTime})")
	@Options(useGeneratedKeys=true, keyProperty="id")
	Integer takeTask(WdShareTask task);
	
	/**
	 * 查询要接取的任务
	 * @author 刘志远<yuanzhi-2@163.com>
	 * @since 2017年4月4日
	 * @param taskId
	 * @param buyerId
	 */
	@Select("select S.id, S.take_time, M.item_title, M.item_price, M.cashback, M.item_pic, M.item_url, U.qq, M.share_text, M.other, M.share_image_urls, S.comminssion " + 
				"from wd_share_task S inner join " +
					"wd_task M on S.task_id = M.id inner join " + 
					"user U on U.id = M.seller_id " +
				"where S.id=#{taskId} and S.buyer_id = #{buyerId}")
	WdShareTask getShowTask(@Param("taskId") Long taskId, @Param("buyerId") Long buyerId);
	
	@Select("select count(1) from wd_share_task where task_id=#{taskId} and buyer_id=#{buyerId} and status!='CANCLED'")
	Integer takeCount(@Param("taskId") Long taskId, @Param("buyerId") Long buyerId);
	
	Integer countBuyerTask(TaskSearchVo vo);
	
	List<WdShareTask> listBuyerTask(TaskSearchVo vo);
	
	@Delete("delete from wd_share_task where id=#{id} and buyer_id=#{buyerId} and status='CANCLED'")
	void delBuyerTask(@Param("id") Long id, @Param("buyerId") Long buyerId);
	
	/**
	 * 查询任务
	 * @author 刘志远<yuanzhi-2@163.com>
	 * @since 2017年4月4日
	 * @return
	 */
	@Select("select * from wd_share_task where id=#{id} and buyer_id=#{buyerId}")
	WdShareTask getShareTask(@Param("id") Long id, @Param("buyerId") Long buyerId);
	
	/**
	 * 更新任务状态
	 * @author 刘志远<yuanzhi-2@163.com>
	 * @since 2017年4月4日
	 * @param id
	 * @param status
	 */
	@Update("update wd_share_task set status = #{status}, modify_time = sysdate() where id = #{id}")
	void updateTaskStatus(@Param("id") Long id,@Param("status") WdShareTaskStatus status);
	
	/**
	 * 进行任务
	 * @author 刘志远<yuanzhi-2@163.com>
	 * @since 2017年4月4日
	 * @param id
	 * @param picUrl
	 * @param buyerId
	 */
	@Update("update wd_share_task set pic_url = #{picUrl}, status='WAIT_CHECK', modify_time = sysdate() where id=#{id} and buyer_id=#{buyerId} and (status='RECEIVED' or status='SHARE_DISALLOWED')")
	void processWdTask(@Param("id") Long id,@Param("picUrl") String picUrl, @Param("buyerId") Long buyerId);
	
	Integer countWaitCheckTask(WdShareTaskSearchVo vo);
	
	List<WdShareTask> listWaitCheckTask(WdShareTaskSearchVo vo);
	
	@Update("update wd_share_task set status=#{status} where task_id=#{taskId} and id=#{id}")
	void checkShareTask(@Param("taskId") Long taskId, @Param("id") Long id, @Param("status") WdShareTaskStatus status);
	
	@Select("select S.id, M.order_deadline, M.cashback, M.id as task_id from wd_share_task S inner join wd_task M on S.task_id = M.id where S.id=#{id} and S.buyer_id = #{buyerId} and S.status='WAIT_SUBMIT'")
	WdShareTask getWaitSubmitOrderShareTask(@Param("id") Long id, @Param("buyerId") Long buyerId);

	@Select("select * from wd_share_task where  SYSDATE()> DATE_ADD(take_time, INTERVAL +30 MINUTE) and status='RECEIVED'")
	List<WdShareTask> listOverdueTask();

	@Select("select * from wd_share_task WST where WST.status='SUMITED' and not exists(select 1 from wd_order_task WOT where WOT.share_id = WST.id and WOT.status!='REFUNDED')")
	List<WdShareTask> listFinishedTask();

	@Update("update wd_share_task set status='FINISHED' where status='SUMITED' and id=#{id}")
	void finishTask(@Param("id") Long id);
	
	Integer countBuyerWdTaskForSeller(WdShareTaskSearchVo vo);
	
	List<WdShareTask> listBuyerWdTaskForSeller(WdShareTaskSearchVo vo);
	
	@Update("update wd_share_task set cac_pic_url = #{picUrl}, modify_time = sysdate() where id=#{id} and buyer_id=#{buyerId} and status='RECEIVED'")
	void saveWdTaskStepOne(@Param("id") Long id,@Param("picUrl") String picUrl, @Param("buyerId") Long buyerId);

	@Update("update wd_task M, wd_share_task S set S.`status`= 'SUMITED' where  S.task_id = M.id and S.`status` = 'WAIT_SUBMIT' and M.order_deadline<sysdate()")
    void updateLiveTaskStatus();

    Integer countBuyerTaskBalance(SellerBalanceVo vo);

	List<WdShareTask> findBuyerTaskBalance(SellerBalanceVo vo);

	List<WdShareTask> exportSellerBlance(SellerBalanceVo vo);

	@Select("select A.task_id, A.id, C.qq, (select count(1) from wd_order_task D where D.share_id = A.id) as order_count, A.take_time from wd_share_task A left outer join wd_task B on A.task_id = B.id left join user C on C.id = B.seller_id where A.id=#{id} and A.buyer_id=#{buyerId}")
	WdShareTask findBuyerTaskDetailForView(@Param("id") Long id, @Param("buyerId") Long buyerId);

	Integer countTaskForAdmin(TaskSearchVo vo);

	List<BuyerTaskVo> selectListForAdmin(TaskSearchVo vo);

	@Select("select * from wd_share_task where id=#{id}")
	WdShareTask getShareTaskById(@Param("id") Long id);
}
